﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biz_Dal.Biz;

public partial class Admin_Admin_QL_SACH : System.Web.UI.Page
{
    BizQuangCao quangcao;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["Dang_nhap"] != "1")
        {
            Response.Redirect("~/Trang_web/Trang_chu.aspx");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            New();
        string jsScript = "<script language='JavaScript' src='confirm.js'></script>";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", jsScript);
    }
    protected void GridViewQC_SelectedIndexChanged(object sender, EventArgs e)
    {
        update();
        PanelThongTin.Visible = true;
        GridViewRow row = GridViewQC.SelectedRow;
        int id = Convert.ToInt32(row.Cells[0].Text.ToString());
        quangcao = BizQuangCao.getQuangCaoById(id);
        LabelId.Text = id.ToString();
        txtHref.Text = quangcao.HREF.ToString();
        Calendar1.SelectedDate = Convert.ToDateTime(quangcao.ngayBD);
        Calendar2.SelectedDate = Convert.ToDateTime(quangcao.ngayKT);
        Load_anh();
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)//bắt đầu
    {
        txtNgayBD.Text = Calendar1.SelectedDate.ToString();
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)//kết thúc
    {
        txtNgayKT.Text = Calendar2.SelectedDate.ToString();
    }

    private void New()
    {
        insert();
        quangcao = null;
        PanelThongTin.Visible = true;
        int id = BizSach.IdMaxSach();
        id++;
        LabelId.Text = id.ToString();
        txtHref.Text = null;
        txtNgayBD.Text = null;
        txtNgayKT.Text = null;
        Calendar1.SelectedDate = DateTime.Now;
        Calendar2.SelectedDate = DateTime.Now;
        txtNgayBD.Text = Calendar1.SelectedDate.ToString();
        txtNgayKT.Text = Calendar2.SelectedDate.ToString();
    }

    protected void btnXoa_Click(object sender, EventArgs e)
    {
        BizQuangCao.deleteQuangCao( Convert.ToInt32(LabelId.Text.ToString()));
        New();
        Image_qc.ImageUrl = "~/Hinh/images.jpg";
        GridViewQC.DataBind();
    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        New();
    }

    protected void btnLMoi_Click(object sender, EventArgs e)//insert
    {
        quangcao = new BizQuangCao();
        quangcao.idQuangCao = Convert.ToInt32(LabelId.Text.ToString());
        quangcao.HREF = txtHref.Text;
        quangcao.ngayBD = Calendar1.SelectedDate;
        quangcao.ngayKT = Calendar1.SelectedDate;
        BizQuangCao.insertQuangCao(quangcao);
        int id = BizQuangCao.IdMaxQuangCao();
        LabelId.Text = id.ToString();
        if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
        {
            //Read the file in to a byte Array.
            byte[] filebyte = FileUpload1.FileBytes;
            BizQuangCao.updateImage(id, filebyte);
            Load_anh();
        }
        update();
        GridViewQC.DataBind();
    }

    protected void btnCapNhat_Click(object sender, EventArgs e)//update
    {
        quangcao = BizQuangCao.getQuangCaoById(Convert.ToInt32(LabelId.Text));
        quangcao.idQuangCao = Convert.ToInt32(LabelId.Text);
        quangcao.HREF = txtHref.Text;
        quangcao.ngayBD = Calendar1.SelectedDate;
        quangcao.ngayKT = Calendar1.SelectedDate;
        quangcao.updateQuangCao();
        if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
        {
            //Read the file in to a byte Array.
            byte[] filebyte = FileUpload1.FileBytes;
            BizQuangCao.updateImage(Convert.ToInt32(LabelId.Text.ToString()), filebyte);
            Load_anh();
        }
        GridViewQC.DataBind();
    }

    private void Load_anh()
    {
        if (quangcao.hinhQuangCao != null)
            Image_qc.ImageUrl = "~/ImageQC.ashx?ID=" + LabelId.Text;
        else
            Image_qc.ImageUrl = "~/Hinh/images.jpg";
    }

    #region "btn"
    private void insert()
    {
        btnCapNhat.Visible = false;
        btnLMoi.Visible = true;
        btnXoa.Visible = false;
    }
    private void update()
    {
        btnXoa.Visible = true;
        btnCapNhat.Visible = true;
        btnLMoi.Visible = false;
    }
    #endregion

}